package com.ruoyi.ring.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.enums.dict.AiModel;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ora.constant.BizConstants;
import com.ruoyi.ora.domain.ChatRequest;
import com.ruoyi.ora.domain.ChatResponse;
import com.ruoyi.ora.service.IAiInterfaceService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChatHunyuanServiceImpl implements IAiInterfaceService {

    private RestTemplate restTemplate = new RestTemplate();

    private ISysConfigService sysConfigService = SpringUtils.getBean(ISysConfigService.class);

    @Override
    public ChatResponse chatCompletions(ChatRequest chatRequest) {
        String url = sysConfigService.selectConfigByKey(BizConstants.Redis.SysConfig.HUNYUAN_API_URL); // "https://api.hunyuan.cloud.tencent.com/v1/chat/completions";
        String apiKey = sysConfigService.selectConfigByKey(BizConstants.Redis.SysConfig.HUNYUAN_API_KEY);  // "sk-YSM73W3NgLSTFifGwQ8vapXwYfGhphD6poTuKElvwcWOP4dp";

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer "+apiKey);
        headers.setContentType(MediaType.APPLICATION_JSON); // 如果需要设置内容类型
        HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(chatRequest), headers);
        return restTemplate.postForObject(url, entity, ChatResponse.class);
    }

    @Override
    public String chatCompletions(String prompt) {
        return getChatCompletions(prompt).getChoices().get(0).getMessage().getContent();
    }

    @Override
    public ChatResponse getChatCompletions(String prompt) {
        return getChatCompletions(prompt, AiModel.HUNYUAN);
    }

    @Override
    public ChatResponse getChatCompletions(String prompt, AiModel aiModel) {
        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(aiModel.getValue());
        List<ChatRequest.Message> messages = new ArrayList<>();
        ChatRequest.Message message = new ChatRequest.Message();
        message.setRole("user");
        message.setContent(prompt);
        messages.add(message);
        chatRequest.setMessages(messages);
        return chatCompletions(chatRequest);
    }

    @Override
    public String chatImageCompletions(String prompt, String imageUrl) {
        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(AiModel.HUNYUAN_V2.getValue());
        List<ChatRequest.Message> messages = new ArrayList<>();
        ChatRequest.Message message = new ChatRequest.Message();
        message.setRole("user");

        List<ChatRequest.Content> contents = new ArrayList<>();
        ChatRequest.Content text = new ChatRequest.Content();
        text.setType("text");
        text.setText(prompt);
        ChatRequest.Content image = new ChatRequest.Content();
        image.setType("image_url");
        image.setImage_url(new ChatRequest().new Image(imageUrl));

        contents.add(text);
        contents.add(image);

        message.setContent(contents);
        messages.add(message);
        chatRequest.setMessages(messages);
        return chatCompletions(chatRequest).getChoices().get(0).getMessage().getContent();
    }

    public static void main(String[] args) {

        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel("hunyuan-vision");
        List<ChatRequest.Message> messages = new ArrayList<>();
        ChatRequest.Message message = new ChatRequest.Message();
        message.setRole("user");

        List<ChatRequest.Content> contents = new ArrayList<>();
        ChatRequest.Content text = new ChatRequest.Content();
        text.setType("text");
        text.setText("请给出图片中食物的脂肪、蛋白质、碳水化合物含量，并以json格式返回，直接给出一个大概数值，单位g，json返回的时候不要包含单位g，不要给范围，直接取中间值就好。json格式为{\"fat\": \"脂肪\", \"protein\": \"蛋白质\", \"carbohydrate\": \"碳水化合物\"}");
        ChatRequest.Content image = new ChatRequest.Content();
        image.setType("image_url");
        image.setImage_url(new ChatRequest().new Image("https://qcloud.dpfile.com/pc/tiZc7qOROI_uHzqw-fMgtqsK6480IY72ig0m8TOK8ZsysgOa3K8QkvHnyD6SUbzw.jpg"));

        contents.add(text);
        contents.add(image);

        message.setContent(contents);
        messages.add(message);
        chatRequest.setMessages(messages);


        String url = "https://api.hunyuan.cloud.tencent.com/v1/chat/completions";
        String apiKey = "sk-YSM73W3NgLSTFifGwQ8vapXwYfGhphD6poTuKElvwcWOP4dp";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer "+apiKey);
        headers.setContentType(MediaType.APPLICATION_JSON); // 如果需要设置内容类型
        HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(chatRequest), headers);
        RestTemplate restTemplate = new RestTemplate();
        ChatResponse chatResponse = restTemplate.postForObject(url, entity, ChatResponse.class);

        System.out.println(chatResponse.getChoices().get(0).getMessage().getContent());
        System.out.println(JSONObject.toJSONString(chatResponse));


        /**
         *
         *
         * curl --location 'https://api.hunyuan.cloud.tencent.com/v1/chat/completions' \
         * -H "Content-Type: application/json" \
         * -H "Authorization: Bearer $HUNYUAN_API_KEY" \
         * --data '{
         *   "model": "hunyuan-vision",
         *   "messages": [
         *         {
         *             "role": "user",
         *             "content": [
         *                 {
         *                     "type": "text",
         *                     "text": "What'\''s in this image?"
         *                 },
         *                 {
         *                     "type": "image_url",
         *                     "image_url": {
         *                         "url": "https://qcloudimg.tencent-cloud.cn/raw/42c198dbc0b57ae490e57f89aa01ec23.png"
         *                     }
         *                 }
         *             ]
         *         }
         *     ]
         * }'
         *
         *
         *
         */
    }
}
